package com.zjlanyun.erp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.yolanda.nohttp.rest.Response;
import com.zjlanyun.erp.config.AppConfig;
import com.zjlanyun.erp.interior.InteriorMainGrid;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.nohttp.HttpRequest;
import com.zjlanyun.erp.nohttp.SimpleHttpListener;
import com.zjlanyun.erp.utils.Toastor;
import com.zjlanyun.erp.view.BaseSwipeBackActivity;

import org.json.JSONObject;

import butterknife.Bind;

public class Login extends BaseSwipeBackActivity {

    @Bind(R.id.btn_bindOffline)
    Button btn_bindOffline;
    @Bind(R.id.btn_login)
    Button btn_login;
    @Bind(R.id.et_username)
    EditText et_username;
    @Bind(R.id.et_password)
    EditText et_password;
    @Bind(R.id.et_customer_number)
    EditText et_customer_number;

    private String username, password, customer_number, FEmpNO, FNumber, moduleNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ((MainApplication) getApplication()).addActivity(this);
        initBaseView();
        initView();
    }

    // 初始化view
    private void initView() {
        title.setText(R.string.login);

        if (!AppConfig.getInstance().getString("username", "").equals("")) {
            et_username.setText(AppConfig.getInstance().getString("username", ""));
        }
        if (!AppConfig.getInstance().getString("customer_number", "").equals("")) {
            et_customer_number.setText(AppConfig.getInstance().getString("customer_number", ""));
        }

        // 登录
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = et_username.getText().toString();
                password = et_password.getText().toString();
                customer_number = et_customer_number.getText().toString();

                if (customer_number.equals("")) {
                    Toastor.getInstance().showShortToast(R.string.input_customer_number);
                } else if (username.equals("")) {
                    Toastor.getInstance().showShortToast(R.string.input_username);
                } else {
                    login();
                }
            }
        });

        //离线绑定
        btn_bindOffline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customer_number = et_customer_number.getText().toString();
                if (customer_number.equals("")) {
                    Toastor.getInstance().showShortToast(R.string.input_customer_number);
                } else {
                    bindOffline();
                }
            }
        });
    }

    /**
     * 绑定设备
     */
    private void bind() {
        HttpRequest client = new HttpRequest(mContext, AppConfig.getInstance().getString("device_bind_url", ""));
        client.getRequest().add("device_token", AppConfig.getInstance().getString("device_token", ""));
        client.getRequest().add("customer_number", customer_number);
        client.getRequest().add("erp_username", username);
        client.getRequest().add("device_type", "android");
        client.send(new SimpleHttpListener<JSONObject>() {
            @Override
            public void onSucceed(int what, Response<JSONObject> response) {
                AppConfig.getInstance().putString("customer_number", customer_number);
                AppConfig.getInstance().putString("username", username);
                AppConfig.getInstance().putString("password", password);
                AppConfig.getInstance().putBoolean("isLogin", true);
                AppConfig.getInstance().putInt("usertype", Data.USER_TYPE_INTERIOR);
                AppConfig.getInstance().putString("FEmpNO", FEmpNO);
                AppConfig.getInstance().putString("FNumber", FNumber);
                AppConfig.getInstance().putString("moduleNum", moduleNum);
                AppConfig.getInstance().putBoolean("isBind", true);
                //企业内部
//                startActivity(new Intent(Login.this, InteriorMainActivity.class));
                startActivity(new Intent(Login.this, InteriorMainGrid.class));

                Home.mActivity.finish();
                finish();
            }
        });
    }

    /**
     * 离线绑定
     */
    private void bindOffline() {
        HttpRequest client = new HttpRequest(mContext, AppConfig.getInstance().getString("device_bind_url", ""));
        client.getRequest().add("device_token", AppConfig.getInstance().getString("device_token", ""));
        client.getRequest().add("customer_number", customer_number);
        client.getRequest().add("erp_username", "");
        client.getRequest().add("device_type", "android");
        client.send(new SimpleHttpListener<JSONObject>() {
            @Override
            public void onSucceed(int what, Response<JSONObject> response) {
                AppConfig.getInstance().putString("customer_number", customer_number);
                AppConfig.getInstance().putBoolean("isBind", true);
                Toastor.getInstance().showShortToast(R.string.bind_success);
            }
        });
    }

    /**
     * 登录ERP
     */
    private void login() {
        HttpRequest client = new HttpRequest(mContext, WebApi.user());
        client.getRequest().add("action", "login");
        client.getRequest().add("username", username);
        client.getRequest().add("password", password);
        client.getRequest().add("usertype", Data.USER_TYPE_INTERIOR);
        client.send(new SimpleHttpListener<JSONObject>() {
            @Override
            public void onSucceed(int what, Response<JSONObject> response) {
                JSONObject data = response.get().optJSONObject("data");
                FEmpNO = data.optString("FEmpNO");
                FNumber = data.optString("FNumber");
                moduleNum = data.optString("moduleNum");
                if (AppConfig.getInstance().getBoolean("isBind", false)) {
                    AppConfig.getInstance().putString("username", username);
                    AppConfig.getInstance().putString("password", password);
                    AppConfig.getInstance().putBoolean("isLogin", true);
                    AppConfig.getInstance().putInt("usertype", Data.USER_TYPE_INTERIOR);
                    AppConfig.getInstance().putString("FEmpNO", FEmpNO);
                    AppConfig.getInstance().putString("FNumber", FNumber);
                    AppConfig.getInstance().putString("moduleNum", moduleNum);


                    //企业内部
//                    startActivity(new Intent(Login.this, InteriorMainActivity.class));
                    startActivity(new Intent(Login.this, InteriorMainGrid.class));

                    Home.mActivity.finish();
                    finish();
                } else {
                    bind();
                }
            }
        });
    }
}
